<?php
class Mhome extends CI_Model{

	

	function get_categories(){//-- for categories--
		$this->db->select('category_id, category_name');
		$query = $this->db->get('categories');
		return $query->result_array();
	}

	function get_footer(){//--for 10 popular product images of footer
		$this->db->select('product_id, product_name, product_image');
		$this->db->where('product_del', 0);
		$this->db->order_by('no_of_time_viewed', "desc");
		$this->db->limit(10);
		$query = $this->db->get('products');
		return $query->result_array();
		
	}

	function get_slideshow(){//--for slideshow--
		$this->db->where('chosen', 'yes');
		$this->db->order_by('post_at', 'desc');
		$this->db->limit(4);
		$query = $this->db->get('slideshow');
		return $query->result_array();

	}	

	function get_latproducts(){//--for 6 latest products		
		$this->db->where('product_del', 0);
		$this->db->order_by('post_at', 'desc');
		$this->db->limit(6);
		$query = $this->db->get('products');
		return $query->result_array();
	}
	

	function get_about(){//--for about_page--
		$query = $this->db->get('aboutus');
		return $query->result_array();		
	}

	function get_news($limit=Null, $offset=Null){
		$this->db->where('article_del', 0);
		$query = $this->db->get('news',$limit, $offset);
		return $query->result_array();
	}

	function get_total_rows_news(){
		$this->db->select('article_id');
		$this->db->where('article_del', 0);
		$query = $this->db->get('news');
		return $query->num_rows();
	}
	function get_removed_news(){
		$this->db->where('article_del', 1);
		$query = $this->db->get('news');
		return $query->result_array();
	}



	function get_article($article_id){
		$this->db->where('article_id', $article_id);
		$this->db->where('article_del', 0);		
		$query = $this->db->get('news');
		return $query->row_array();
	}

	function get_total_rows_cate(){ //ham nay dem so category co san pham		
		$this->db->where('category_achor', 1);		
		return  $this->db->count_all_results('categories');
		
	}

	function update_cate(){//ham nay de danh dau nhung category nao co san pham
		$query = $this->db->get('categories');
		$categories = $query->result_array();
		foreach ($categories as $category) {
			$this->db->where('category_id', $category['category_id']);			
			$n = $this->db->count_all_results('products');			
			if($n>0){
				$this->db->where('category_id', $category['category_id']);
        		$this->db->update('categories', array('category_achor'=>1));
			}

		} 
	}

	function get_blog($limit, $offset){//---for blog_page----
		// This func gets all the products according to the categories
		// get teh categories		
		$this->db->where('category_achor', 1);			
		$query = $this->db->get('categories',$limit, $offset);
		$array = $query->result_array();	
		foreach ($array as $key=>$cat) {
			//get the products
			$return[$key]['cat'] = $cat['category_name'];
			$this->db->where('category_id', $cat['category_id']);
			$this->db->where('product_del', 0);
			$this->db->limit(2);
			$query = $this->db->get('products');			
			$return[$key]['product'] = $query->result_array();				
		}
		return $return;
	}

	function content_categories($category_id){//--all the products according to the categories--
		$this->db->select('category_name');
		$this->db->where('category_id', $category_id);
		$query_cat = $this->db->get('categories');
		$return['cat'] = $query_cat->row_array();		
		$this->db->select('product_id, product_image, product_name, post_at, no_of_time_viewed, product_article');
		$this->db->where('category_id', $category_id);
		$this->db->where('product_del', 0);
		$query = $this->db->get('products');
		$return['product'] = $query->result_array();				
		return $return;
		}
		
	 function get_all_product($limit, $offset){//--for product_page--
		$this->db->where('product_del', 0);	
		$query = $this->db->get('products', $limit, $offset);
		return $query ->result_array();
	}


	 function get_product($product_id){//--for single_product_page--
		//func gets the single product accord to the id
		
		$this->db->where('product_id', $product_id);
		$this->db->where('product_del', 0);
		$query = $this->db->get('products');
		//increase the number of view for this product
		$array = $query->row_array();		
		$new_views = $array['no_of_time_viewed']+1 ;
		$data = array('no_of_time_viewed' => $new_views);
		$this->db->where('product_id', $product_id);
		$this->db->update('products', $data);
		//increase the number of view for the categories
		$this->db->select('no_of_time_viewed');
		$this->db->where('category_id', $array['category_id']);
		$query_cat = $this->db->get('categories');
		$newarray = $query_cat->row_array();
		$newdata = array('no_of_time_viewed' => $newarray['no_of_time_viewed']+1);
		$this->db->where('category_id', $array['category_id']);
		$this->db->update('categories', $newdata);
		return $query ->row_array();
	   
	}


	function get_total_rows(){		
		$this->db->where('product_del', 0);		
		return $this->db->count_all_results('products');		
	}
	

	function contact_us(){
		//$this function sends an email to the admin and send a
		// confirmation email to the user as well
		
		$this->email->from('ctcj82@gmail.com', 'Admin');
		$this->email->to('ctcj82@gmail.com');
		$this->email->subject($this->input->post('subject'));
		$message = "Name". $this->input->post('full_name').'\n'.
					"Email" .$this->input->post('email').'\n'.
					 "Phone" .$this->input->post('phone').'\n'.
					"Company Name". $this->input->post('company_name').'\n'.
					"Message" . $this->input->post('message');
		$this->email->message($message)	;
		$this->email->clear();
		$this->email->from('ctcj82@gmail.com');
		$this->email->to('ctcj82@gmail.com');
		$this->email->subject('Contact us form was filled');
		$newmessage = "thanks you for filling the contact us form on
		the website: msenterprises.com". $this->input->post('full_name').'\n'.
					"We will be contacting you shortly. Thank you" .$this->input->post('email').'\n'.
					 "Phone" .$this->input->post('phone').'\n'.
					"Company Name". $this->input->post('company_name').'\n'.
					"Message" . $this->input->post('message');
		$this->email->message($newmessage) ;
		$this->email->send();
		
	}


//--------------------------start admin page------------------------------------


	function get_cat_name($category_id){
		$this->db->select('category_name');
		$this->db->where('category_id', $category_id);
		$query = $this->db->get('categories');
		$array = $query->row_array();
		return $array['category_name'];
	}

	function admin_get_products($limit=Null, $offset=NUll){
		$this->db->where('product_del', 0);
		$query = $this->db->get('products',$limit, $offset);
		$array = $query->result_array();
		foreach ($array as $key => $value) {
			$array[$key]['category_name'] = $this->mhome->
			get_cat_name($array[$key]['category_id']);
		}
		return $array;
	}

	
	function admin_get_remove_products(){
		$this->db->where('product_del', 1);
		$query = $this->db->get('products');
		$array = $query->result_array();
		foreach ($array as $key => $value) {
			$array[$key]['category_name'] = $this->mhome->
			get_cat_name($array[$key]['category_id']);
		}
		return $array;
	}



    function admin_get_product($product_id){
    	$this->db->where('product_id', $product_id);
    	$this->db->where('product_del', 0);
    	$query = $this->db->get('products');
    	$array = $query->row_array();
    	$array['cat_name']= $this->mhome->get_cat_name($array['category_id']);    	
    	return $array;
    }	

    	//func to delete a product according to the product_id
    function delete_product($product_id){
		 
		$this->db->where('product_id', $product_id);
		$this->db->delete('products');

    }
    	
    function restore_product($product_id)	{
		$this->db->where('product_id', $product_id);
    	$this->db->update('products', array('product_del'=>0));
    }
		
	function restore_article($article_id){
		$this->db->where('article_id', $article_id);
		$this->db->update('news',array('article_del'=>0));
	}	

	function remove_product($product_id){
		$this->db->where('product_id', $product_id);
    	$this->db->update('products', array('product_del'=>1));
	}

	function remove_article($article_id){
		$this->db->where('article_id', $article_id);
		$this->db->update('news', array('article_del'=>1));
	}


	function add_a_category(){
    		$this->form_validation->set_rules('category_name', 'Category name',
    		 'required|xss_clean|max_length[100]|is_unique[categories.category_name]');
    		if($this->form_validation->run()==FALSE){
    			return FALSE;
    		}
    		else{
    			$data = array(
    					'category_name'=>$this->input->post('category_name'),
    					'no_of_time_viewed'=>0
    				);
    			$this->db->insert('categories', $data);
    			return TRUE;
    		}
    	}

    	//func to edit a category

    //func  to get a category based on the category_id	
    function get_a_category($category_id){
    	$this->db->where('category_id', $category_id);
    	$query = $this->db->get('categories');
    	return $query->row_array();
    }

    //func to save an edited category
    function save_edited_cat(){
    	$this->form_validation->set_rules('category_name', 'category name', 
    		'required|xss_clean|max_length[100]|is_unique[categories.category_name]');
    	if($this->form_validation->run()==FALSE){
    		return FALSE;
    	}
    	else{
    		$data = array(
    					   'category_name'=>$this->input->post('category_name')	
    			);
    		$this->db->where('category_id', $this->input->post('category_id'));
    		$this->db->update('categories', $data);
    		return TRUE;
    	}
    }

    //func to delete a category
    function delete_category($category_id){

    	$this->db->where('category_id', $category_id);
    	$query = $this->db->get('products');    	     	
    	if($query->num_rows>0){
    		return FALSE;
        }
        else{
      	$this->db->where('category_id', $category_id);    		
    	$this->db->delete('categories');
    	return TRUE;
        }
    }

    function delete_article($article_id){
    	$this->db->where('article_id', $article_id);
    	$this->db->delete('news');
    }



	function save_edited_product(){
        $this->form_validation->set_rules('product_name', 'Product Name', 'required|xss_clean');
        $this->form_validation->set_rules('product_price', 'Product Price', 'required|xss_clean');
        $this->form_validation->set_rules('description', 'Product Description', 'required|xss_clean');
        if($this->form_validation->run() == FALSE){
        	return FALSE;
        }
      	else{
        
        //if main image has been sent save the image and get the filename
        if($_FILES['product_image']["size"]>0){
         $config['upload_path'] = "./public/images/";
          $config['allowed_types'] = 'gif|jpg|png';
          $config['max_size'] = '100000';          
          $this->upload->initialize($config);
          
          if ( !$this->upload->do_upload("product_image"))
          {  $this->upload->display_errors;
            return FALSE;
          }
          else{
             $upload_data = $this->upload->data();
          }
     	 }

        if(isset($upload_data)){ $product_image = $upload_data['file_name'];}
        else { $product_image = $this->input->post('product_image');}     
        $product_id = $this->input->post('product_id');
        $data = array(
        			'product_label'=>$this->input->post('product_label'),
                    'product_name'=>$this->input->post('product_name'),
                    'product_price'=>$this->input->post('product_price'),                
                    'product_description'=>$this->input->post('description'),
                    'product_article'=>$this->input->post('product_article'),
                    'category_id'=>$this->input->post('category_id'),
                    'product_image'=>$product_image
                      );
        //update the product id row and return TRUE
        $this->db->where('product_id', $product_id);
        $this->db->update('products', $data);
        return TRUE;

     	 }

    	}
    	
    
    function save_add_product(){
		$this->form_validation->set_rules('product_name', 'Product Name', 'required|xss_clean');
      	$this->form_validation->set_rules('product_price', 'Product Price', 'required|xss_clean');
      	$this->form_validation->set_rules('description', 'Product Description', 'required|xss_clean');
     	$this->form_validation->set_rules('product_label', 'Product label', 'required|xss_clean|is_unique[Products.product_label]'); 
     	$this->form_validation->set_rules('product_article', 'Product article', 'required|xss_clean');
     	 if($this->form_validation->run() == FALSE){
        return FALSE;
   	 	}
     	else
     	{        
	        //if main image has been sent save the image and get the filename
	        if($_FILES['product_image']["size"]>0){
         $config['upload_path'] = "./public/images/";
          $config['allowed_types'] = 'gif|jpg|png';
          $config['max_size'] = '100000';         
          $this->upload->initialize($config);
          
          if ( !$this->upload->do_upload("product_image"))
          {  $this->upload->display_errors;
            return FALSE;
          }
          else 
          {
            $upload_data = $this->upload->data();            

          }
        
       }
        if(isset($upload_data)){ $product_image = $upload_data['file_name'];}
        else { $product_image = 'defaul.jpg';}     
	      
	        $product_data_array = array(
						        		'product_label'=>$this->input->post('product_label'),
					                    'product_name'=>$this->input->post('product_name'),
					                    'product_price'=>$this->input->post('product_price'),                
					                    'product_description'=>$this->input->post('description'),
					                    'product_article'=>$this->input->post('product_article'),
					                    'category_id'=>$this->input->post('category_id'),
					                    'product_image'=>$product_image,					                    
					                    'no_of_time_viewed'=>0,
					                    'post_at'=>time(),
					                    'product_del'=>0
					                  );
        	$this->db->insert('products', $product_data_array);
        	return TRUE;

	     	 }
    	}

    	//func to add a category
    

    function save_add_article(){
		$this->form_validation->set_rules('article_name', 'Article Name', 'required|xss_clean');	      	
      	$this->form_validation->set_rules('article_content', 'Article Content', 'required|xss_clean');	     	
     	$this->form_validation->set_rules('article_sourse', 'Article Sourse', 'required|xss_clean');
     	 if($this->form_validation->run() == FALSE){
        return FALSE;
   	 	}
     	else
     	{        
        //if main image has been sent save the image and get the filename
        if($_FILES['article_image']["size"]>0){
          $config['upload_path'] = "./public/images/";
          $config['allowed_types'] = 'gif|jpg|png';
          $config['max_size'] = '100000';         
          $this->upload->initialize($config);
      
        if ( !$this->upload->do_upload("article_image")){ 
          $this->upload->display_errors;
         	return FALSE;
        } 
        else {
        $upload_data = $this->upload->data();            

        }
        
        }
        if(isset($upload_data)){ $article_image = $upload_data['file_name'];}
        else { $article_image = 'defaul.jpg';}     
	      
	        $article_data_array = array(
						        		
					                    'article_name'=>$this->input->post('article_name'),
					                    'article_content'=>$this->input->post('article_content'),
					                    'article_sourse'=>$this->input->post('article_sourse'),
					                    'article_image'=>$article_image,					                    
					                    'article_view'=>0,
					                    'article_like'=>0,
					                    'article_post'=>time(),
					                    'article_del'=>0
					                  );
	        	$this->db->insert('news', $article_data_array);
	        	return TRUE;

	     	 }
    	}

	function save_edit_article(){

		$this->form_validation->set_rules('article_name', 'Article Name', 'required|xss_clean');	      	
      	$this->form_validation->set_rules('article_content', 'Article Content', 'required|xss_clean');	     	
     	$this->form_validation->set_rules('article_sourse', 'Article Sourse', 'required|xss_clean');
     	 if($this->form_validation->run() == FALSE){
        return FALSE;
   	 	}
     	else
     	{        
        //if main image has been sent save the image and get the filename
        if($_FILES['article_image']["size"]>0){
          $config['upload_path'] = "./public/images/";
          $config['allowed_types'] = 'gif|jpg|png';
          $config['max_size'] = '100000';         
          $this->upload->initialize($config);
      
       if ( !$this->upload->do_upload("article_image"))
       {  $this->upload->display_errors;
        return FALSE;
       }
       else 
       {
        $upload_data = $this->upload->data();            

      }
    
   }

    if(isset($upload_data)){ $article_image = $upload_data['file_name'];}
    else { $article_image = $this->input->post('article_image');}    
    $article_id = $this->input->post('article_id'); 
      
        $data = array(
					        		
				                    'article_name'=>$this->input->post('article_name'),
				                    'article_content'=>$this->input->post('article_content'),
				                    'article_sourse'=>$this->input->post('article_sourse'),
				                    'article_image'=>$article_image,					                    
				                    
				                  );
        $this->db->where('article_id', $article_id);
        $this->db->update('news', $data);
        return TRUE;

     	}
	}



 }

?>

